<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Common extends MY_Controller {

	/**
	 * 商家model
	 * @var MerchantUserModel
	 */
	public $mMerchant;
	public function __construct(){
		parent::__construct();		
	}
	
	/**
	 * 搜索用户
	 */
	public function searchUserName(){
		$keyword = trim(get('keyword'));
		
		$this->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
		$tA = $this->mMerchant->getInfoByLikeName($keyword);
        array_unshift($tA, ['id' => '', 'name' => '请选择']);
		echo json_encode($tA);
	}

	/**
	 * 搜索用户
	 */
	public function searchProductName(){
		$keyword = trim(get('keyword'));
		$merchantId = (int)get('merchantId');
        if (!$merchantId){
            echo json_encode(['id' => '', 'name' => '请选择']);exit();
        }
		$this->loadModel('ConsumerProductModel', 'mProduct', 'consumer');
		$tA = $this->mProduct->getInfoByLikeName($keyword, $merchantId);
        array_unshift($tA, ['id' => '', 'name' => '请选择']);
		echo json_encode($tA);
	}

    /**
     * 搜索店铺商品
     * @return array
     */
    public function searchProductByKeyword()
    {
        $keyword = trim(get('keyword'));
        $merchantId = trim(get('merchant_id'));
        if (!$keyword) return array();
        $this->loadModel('ConsumerProductModel', 'mProduct', 'consumer');
        $tA = $this->mProduct->searchProductByKeyword($keyword,$merchantId);
        echo json_encode($tA);
    }
	
	/**
	 * 搜索用户
	 */
	public function getCustomList(){
		$merchantId = get('merchant_id');
		
		$this->loadModel('ConsumerCategoryModel', 'mCategory', 'consumer');
		$tA = $this->mCategory->getCustomList($merchantId);
		echo json_encode($tA);
	}

    /**
     * 区域联动
     */
    public function ajaxChangeCheck(){
        $this->loadModel('AdmSettingModel', 'mAdm', 'admin');

        $parent_id = intval(post('parent_id'));

        $arr = $this->mAdm->getLocationByPid($parent_id);
        echo json_encode($arr);
        exit;
    }

    /**
     * 平台分类联动
     */
    public function getCategoryList(){
        $this->loadModel('ConsumerCategoryModel', 'mCategory', 'consumer');
        $parent_id = intval(post('parent_id'));
        $type = intval(post('type'));
        if ($type == 1) {
            $list = $this->mCategory->getCategoryList($parent_id, true);
        }else{
            $list = $this->mCategory->getCustomList(0,  $parent_id);
        }

        echo json_encode($list);
        exit;
    }
}
	
